package com.load.utils;

import java.io.*;
import java.util.*;

public class ContentUtil {

	public static List<String> readLines(String text) {
		if (text != null && text.length() > 0) {
			String[] linesArray = text.split("\n");
			return Arrays.asList(linesArray);
		}
		return new ArrayList<String>();
	}

	public static String cleanContentNoOO(String content) {
		String newText = "";
		List<String> lines = readLines(content);
		for (String line : lines) {
			line = line.trim();
			if (line.length() > 0) {
				newText += line + "\n";
			}
		}
		return newText.length() > 1 ? newText.substring(0, newText.length() - 1) : newText;
	}

	public static String cleanContentOO(String content) throws IOException {
		if (content != null && !content.equals("")) {
			String newText = "";
			content = replaceInvalidCharacteresOO(content);
			List<String> lines = readLines(content);
			for (String line : lines) {
				line = line.trim();
				if (line.length() > 0 && isValidLineOO(line)) {
					newText += line + "\n";
				}
			}
			return newText.length() > 1 ? newText.substring(0, newText.length() - 1) : newText;
		}
		return "";
	}

	private static boolean isValidLineOO(String text) {
		boolean isParentheses = text.matches("[\\(]+") || text.matches("[\\)]+");
		boolean isBrace = text.matches("[\\{]+") || text.matches("[\\}]+") || text.matches("[\\};]+") || text.equals("});") || text.equals("})");
		boolean isAnnotation = text.startsWith("@");
		return !isParentheses && !isBrace && !isAnnotation;
	}

	private static String replaceInvalidCharacteresOO(String text) {
		text = text.replace("\u0000", "");
		text = text.replace("\u000B", "");
		text = text.replace("\u000E", "");
		text = text.replace("\uFFFE", "");
		text = text.replace("\\f", "");
		return text;
	}

}
